<?php
class Admin_AclpermissionController extends Custom_Controller_Action
{
    public function init()
    {
        
    }
    public function indexAction()
    {
        $userroles = new User_Model_DbTable_Role();
        $usermodule = new User_Model_DbTable_AclResource();
        $objresource = new User_Model_DbTable_AclResource();
        $objpermission = new User_Model_DbTable_Aclpermission();
        $modules = $usermodule->getModulesAll();
        $roles = $userroles->getAll();
        $this->view->roles = $roles;
        $this->view->modules = $modules;
        
        if($this->isAjax)
        {
            if ($this->getRequest()->getParam('op') == 'resource') 
            {
                $this->_helper->viewRenderer->setNoRender();
                
                $modulename = $this->getRequest()->getParam('mod');
                $resource = $objresource->getResourceByModulename($modulename);
                echo Custom_External::jsonEncode($resource);
            }
            if ($this->getRequest()->getParam('op') == 'permission')
            {
                 $this->_helper->viewRenderer->setNoRender();
                $objreflecter = new Custom_Action_Helper_Reflector();
                $resource=  explode("::",$this->getRequest()->getParam('resource'));
                $controller = $resource[0];
                $module = $this->getRequest()->getParam('modules');
                
                $temp = $objreflecter->getList($module);
                $permissions['available'] = $temp[$module][$controller];
                unset($temp);
                
                
                //getting the assigned permissions
                $fields = array('id','role_id','resource_id','permission');
                $permissions['assigned'] = $objpermission->getByColumn('resource_id', $resource[1], $fields);
            
                
                echo Custom_External::jsonEncode($permissions);
            }  
        }        
    
    }
    
    public function addAction()
    {
         $this->_helper->viewRenderer->setNoRender();
         if($this->getRequest()->isPost())
         {     
            $objpermission = new User_Model_DbTable_Aclpermission(); 
           $data =  $this->getRequest()->getParams();
          
           $resourceid = explode("::",$data['resource']);
           foreach($data['availableperm'] as $d)
           {
               if(!$data['role']) $data['role'] = User_Model_DbTable_Role::REGISTER_USER;
               $array['role_id'] = $data['role'];
               $array['resource_id'] = $resourceid[1];
               $array['permission'] = $d;
               $array['created_date'] = date("Y-m-d H:i:s");
               $res = $objpermission->insert($array);
           }    
            
         } 
    }
    
    public function deleteAction()
    {
        $this->_helper->viewRenderer->setNoRender();
         if($this->getRequest()->isPost())
         {     
            $objpermission = new User_Model_DbTable_Aclpermission(); 
           $data =  $this->getRequest()->getParams();
          
           $resourceid = explode("::",$data['resource']);
           foreach($data['assignedperm'] as $d)
           {
               if(!$data['role']) $data['role'] = User_Model_DbTable_Role::REGISTER_USER;
               $res = $objpermission->delete("role_id=".$data['role']." AND resource_id=".$resourceid[1]." AND permission='$d'");
           }    
            
         } 
        
    }
 
  
}

?>